﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;

using EHSubmit.Logic.LINQ;

/// <summary>
/// 答题记录管理
/// </summary>
public class AnswerManager
{
	public AnswerManager()
	{
		
	}


    /// <summary>
    /// 用户答题记录入库，并更新Question的正确率
    /// </summary>
    /// <param name="answers"></param>
    public void AddAnswers(IList<Answer> answers,int goalId)
    {
        EHSubmitDataContext db = new EHSubmitDataContext(EHSubmit.Util.SqlHelper.ConnectionStringLocalTransaction);

        foreach (Answer an in answers)
        {
            //var post = ctx.Posts.Where(p => p.PostId = @postId).Single();  

            var goalQues = db.GoalQuestion.Where(gq => gq.GoalID == goalId && gq.QuestionID == an.QuestionID).Single();
            var ques = db.Question.Where(q => q.QID == an.QuestionID).Single();

            if (an.IsRight == true)
            {
                goalQues.RightCount++;
                ques.RightCount++;
            }
            else
            {
                goalQues.WrongCount++;
                ques.WrongCount++;
            }

            goalQues.Rate = (float)goalQues.RightCount / (goalQues.RightCount + goalQues.WrongCount);
            ques.Rate = (float)ques.RightCount / (ques.RightCount + ques.WrongCount);

            db.Answer.InsertOnSubmit(an);
        }
        
        //db.Answer.InsertAllOnSubmit(answers);

        db.SubmitChanges();
    }
}
